package com.fishery.processing.service.impl;

import com.fishery.entity.GlobalException;
import com.fishery.entity.StatusCode;
import com.fishery.processing.dao.QualificationDao;
import com.fishery.processing.entity.Qualification;
import com.fishery.processing.mapper.QualificationMapper;
import com.fishery.processing.service.QualificationService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author ${author}
 * @since 2021-01-14
 */
@Service
public class QualificationServiceImpl extends ServiceImpl<QualificationMapper, Qualification> implements QualificationService {

    @Autowired
    private QualificationDao qualificationDao;

    public int createQualification(Qualification qualification) {
        List<Qualification> qualifications = qualificationDao.findQualificationOfProduct(qualification.getProductId());
        if (!qualifications.isEmpty()){
            throw new GlobalException(StatusCode.DUPLICATE,"该加工产品的资质信息已存在，无法添加");
        }
        ////id置空 等待mybatis-plus填充
        qualification.setId(null);
        return qualificationDao.addQualification(qualification);
    }

    public int updateQualification(Qualification qualification) {
        return qualificationDao.updateQualification(qualification);
    }

    public int deleteQualification(String qualificationId) {
        return qualificationDao.removeQualification(qualificationId);
    }

    public Qualification searchQualificationById(String qualificationId) {
        return qualificationDao.findQualificationById(qualificationId);
    }

    //根据加工产品id获取产品资质
    public Qualification searchQualificationOfProduct(String productId) {
        List<Qualification> qualifications = qualificationDao.findQualificationOfProduct(productId);
        return qualifications.isEmpty() ? null : qualifications.get(0);
    }
}
